package com.hjy.order.web;

import com.hjy.order.service.OrderService;
import com.hjy.order.vo.OrderConfirmVo;
import com.hjy.order.vo.OrderSubmitVo;
import com.hjy.order.vo.SubmitOrderResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class OrderWebController {

    @Autowired
    private OrderService orderService;

    /**
     * 确认页
     * @param model
     * @return
     */
    @RequestMapping("/toTrade")
    public String toTrade(Model model) {
        OrderConfirmVo orderConfirmVo = orderService.confirmOrder();
        model.addAttribute("confirmOrderData",orderConfirmVo);
        return "confirm";
    }

    /**
     * 订单提交
     * @param orderSubmitVo
     * @param model
     * @return
     */
    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo orderSubmitVo ,Model model) {

        SubmitOrderResponseVo submitOrderResponseVo = orderService.submitOrder(orderSubmitVo);
        if (submitOrderResponseVo.getCode() == 0) {
            // 下单成功来到支付选择页
            model.addAttribute("submitOrderResp",submitOrderResponseVo);
            return "pay";
        }else {
            return "redirect:http://order.gulimall.com/toTrade";
        }
    }
}
